package com.paypalpay.turntointerfaceservice.newservice;

import com.paypalpay.entity.common.OrderPaymentManager;
import com.paypalpay.util.JaxbUtil;
import com.paypalpay.util.wx.HttpClientTool;
import com.paypalpay.xmlpojo.Constant;
import com.paypalpay.xmlpojo.TurntoInterfaceUtils;
import com.paypalpay.xmlpojo.request.RequestBody.OrderMonthlyPackageRequestBody;
import com.paypalpay.xmlpojo.request.RequestHeader;
import com.paypalpay.xmlpojo.request.subRequest.OrderMonthlyPackageRequest;
import com.paypalpay.xmlpojo.response.subResponse.OrderMonthlyPackageResponse;

public class OrderServiceNew {
    public OrderPaymentManager orderPackage(String partnerCode,String deviceId,  String packageName, String packageCode, String packageType, String orderPeriod, String amount, String secret) {
        try {
            // 请求参数的处理
            OrderMonthlyPackageRequestBody body = new OrderMonthlyPackageRequestBody();
            body.setDeviceId(deviceId);
            body.setGlobalOrder("0");
            body.setPackageName(packageName);
            body.setPackageCode(packageCode);
            body.setPackageType(packageType);
            body.setOrderPeriod(orderPeriod);
            body.setPayAmount(amount);
            body.setPayRst("1");//未支付
            body.setPayType("1");//随意支付方式
            body.setIsOpen("0");//下单时上发不开通的报文
            body.setTestFlowFlag("0");
            // 请求参数拼接
            OrderMonthlyPackageRequest request = new OrderMonthlyPackageRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_orderPackage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            System.out.println("签名是：" + sign);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(OrderMonthlyPackageRequest.class).toXml(request, "utf-8");
            System.out.println("下单请求内容：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            System.out.println("下单返回结果：" + result);

            OrderMonthlyPackageResponse responseObj = new JaxbUtil(OrderMonthlyPackageResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
                System.out.println("信息是什么:" + retCode);
                // 成功:北京回传的orderId
                String orderId = responseObj.getBody().getOrderId();
                System.out.println("回传的orderId:" + orderId);
                String startData = responseObj.getBody().getOrderStartDate();
                String expireDate = responseObj.getBody().getOrderExpireDate();

                OrderPaymentManager user = new OrderPaymentManager();
                user.setOrderId(orderId);
                user.setOrderStartDate(startData);
                user.setOrderExpireDate(expireDate);
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;

            } else {
                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;

            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;

    }
}
